﻿Imports System.ComponentModel

Public Enum StatusImage
    Huidig
    InWacht
    Afgerekend
End Enum

Public Class StatusColumn
    Inherits DataGridViewColumn

    Public Sub New()
        MyBase.New(New StatusCell())
    End Sub

    Private m_DefaultStatus As StatusImage = StatusImage.Afgerekend

    Public Property DefaultStatus() As StatusImage
        Get
            Return m_DefaultStatus
        End Get
        Set(ByVal value As StatusImage)
            m_DefaultStatus = value
        End Set
    End Property

    Public Overloads Overrides Function Clone() As Object
        Dim col As StatusColumn = TryCast(MyBase.Clone(), StatusColumn)
        col.DefaultStatus = m_DefaultStatus
        Return col
    End Function

    Public Overloads Overrides Property CellTemplate() As DataGridViewCell
        Get
            Return MyBase.CellTemplate
        End Get
        Set(ByVal value As DataGridViewCell)
            If (value Is Nothing) OrElse Not (TypeOf value Is StatusCell) Then
                Throw New ArgumentException("Invalid cell type, StatusColumns can only contain StatusCells")
            End If
        End Set
    End Property

End Class

Public Class StatusCell
    Inherits DataGridViewImageCell

    Public Sub New()
        Me.ImageLayout = DataGridViewImageCellLayout.Normal
        Me.Style.Alignment = DataGridViewContentAlignment.BottomCenter
    End Sub

    Protected Overloads Overrides Function GetFormattedValue(ByVal value As Object, ByVal rowIndex As Integer, _
             ByRef cellStyle As DataGridViewCellStyle, ByVal valueTypeConverter As TypeConverter, _
             ByVal formattedValueTypeConverter As TypeConverter, ByVal context As DataGridViewDataErrorContexts) _
             As Object

        Dim img As Image = My.Resources.Afgerekend
        Dim status As StatusImage = StatusImage.Afgerekend
        ' Try to get the default value from the containing column 
        Dim owningCol As StatusColumn = TryCast(OwningColumn, StatusColumn)
        If owningCol IsNot Nothing Then
            status = owningCol.DefaultStatus
        End If
        If TypeOf value Is StatusImage OrElse TypeOf value Is Integer Then
            status = DirectCast(value, StatusImage)
        End If
        Select Case status
            Case StatusImage.Afgerekend
                img = My.Resources.Afgerekend
            Case StatusImage.Huidig
                img = My.Resources.Huidig
            Case StatusImage.InWacht
                img = My.Resources.Pauze
        End Select
        cellStyle.Alignment = DataGridViewContentAlignment.TopCenter
        Return img
    End Function

End Class
